//
//  searchView.m
//  project-A (pianke)
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "searchView.h"
#import "CellTwoTab.h"
#import  "CellTwoTab2.h"
#import "NetworkingHandle.h"
#import "model2.h"
#import "WebviewControl.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import <DKNightVersion.h>
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface searchView ()<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate>


@property (nonatomic,retain)UITextField *text;

@property (nonatomic,retain)UIButton *button;

@property (nonatomic,retain)UITableView *tabview;

@property (nonatomic,retain)UIView *backview;

@property (nonatomic,retain)NSMutableArray *arr;

@property (nonatomic,retain)NSMutableAttributedString *string;

@property (nonatomic,assign)NSInteger number;

@end

@implementation searchView

-(void)dealloc{
    [_arr release];
    [_text release];
    [_button release];
    [_tabview release];
    [_backview release];
    [_string release];
    
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  creatview];
    self.title = @"搜索";
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    
}
-(void)creatview{
    self.number =2;
    self.backview = [[UIView alloc]init];
     [self.view addSubview:self.backview];
    self.backview.backgroundColor = [UIColor orangeColor];
    self.backview.frame = CGRectMake(0, 64, WIDTH,HEIGHT - 64);
   
    
    
    self.text = [[UITextField alloc]init];
    self.text.backgroundColor = [UIColor whiteColor];
    [self.backview addSubview:self.text];
    self.text.frame = CGRectMake(5, 5, 0.8 * WIDTH - 5, WIDTH / 8 - 10);
   
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(5 + 0.8 *WIDTH, 5, 0.2 * WIDTH - 10, WIDTH / 8  -10);
    NSLog(@"%@", NSStringFromCGRect(self.button.frame));
    
    [self.button setTitle:@"搜索" forState:UIControlStateNormal];
    
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.backview addSubview:self.button];
    self.button.backgroundColor = [UIColor blueColor];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.button.layer.cornerRadius = 5;
    [self.button addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.tabview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0.125 *WIDTH  + 68, WIDTH,HEIGHT -  0.125 * WIDTH) style:UITableViewStylePlain];
    [self.view addSubview:self.tabview];
    self.tabview.delegate = self;
    self.tabview.dataSource = self;
    
     self.tabview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tabview.backgroundColor = [UIColor whiteColor];
    
    self.tabview.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
    
    [self.tabview registerClass:[CellTwoTab class] forCellReuseIdentifier:@"pool1"];
    [self.tabview registerClass:[CellTwoTab2  class] forCellReuseIdentifier:@"pool2"];
     [self.tabview reloadData];
    [self.backview sendSubviewToBack:self.tabview];
    
    [self.backview release];
     [self.text release];
    [self.tabview release];
    
    self.tabview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        NSLog(@"上拉刷新");
        
        NSString *str = [NSString stringWithFormat:@"http://lol.zhangyoubao.com/apis/rest/SearchService/item?page=%ld&keyword=%@&i_=868299012353735&t_=1454056840654&p_=4307&v_=400609&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=15&cha=tencentMartket&u_=&",self.number++,self.text.text];
        
        
        
        
        [NetworkingHandle handleWorkData:str AndBody:nil value:nil handleBlock:^(id result) {
            
            
            NSMutableArray *arrshuju = [result objectForKey:@"data"];
            for (NSDictionary *dic in arrshuju) {
                
                Model2 *model = [[Model2 alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                NSString *str  =   [dic objectForKey:@"id"];
                model.idd = str;
                [self.arr addObject:model];
                
                
                
                [model release];
            }
            NSLog(@"%ld",self.arr.count);
            
            NSLog(@"%@",self.arr);
            
            
            [self.tabview.mj_footer endRefreshing];
            [self.tabview reloadData];
        }];
        
        
    }];
    
    [self.tabview release];





}

-(void)search{
    [self.text endEditing:YES];
    
    NSLog(@"9999999999999999999999999");
    
    NSString *str = [NSString stringWithFormat:@"http://lol.zhangyoubao.com/apis/rest/SearchService/item?page=1&keyword=%@&i_=868299012353735&t_=1454056840654&p_=4307&v_=400609&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=15&cha=tencentMartket&u_=&",self.text.text];
    
    [NetworkingHandle handleWorkData:str AndBody:nil value:nil handleBlock:^(id result) {
        self.arr = [NSMutableArray array];
    
                NSMutableArray *arrshuju = [result objectForKey:@"data"];
        for (NSDictionary *dic in arrshuju) {
            
            Model2 *model = [[Model2 alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            NSString *str  =   [dic objectForKey:@"id"];
            model.idd = str;
            [self.arr addObject:model];
            [model release];
        }
        NSLog(@"%ld",self.arr.count);
        [self.tabview reloadData];
        NSLog(@"%@",self.arr);
        
    }];
 
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return WIDTH * 0.225 + 30;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        Model2 *model = [self.arr objectAtIndex:indexPath.row];
    if ([model.pic_url isEqualToString:@""]) {
        CellTwoTab *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
        self.string =[[NSMutableAttributedString alloc]initWithString:model.title];
      
     
        
        [self.string addAttribute:NSForegroundColorAttributeName
                           value:[UIColor redColor]
                           range:[model.title rangeOfString:self.text.text]];
        
        cell.title.attributedText =self.string;
        
        NSLog(@"%@",cell.title.attributedText);
        
        cell.desc.text = model.desc;
         NSString *str = model.published;
        
        NSTimeInterval time=[str doubleValue];
        NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
        NSLog(@"date:%@",[detaildate description]);
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        
        NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
        
        NSLog(@"%@",currentDateStr);
        cell.time.text =  currentDateStr;
        
        if (![model.weight_new isEqualToString:@"0"]) {
            
            cell.image.hidden = NO;
            
            
            
        }
        
        return cell;
    }else{
            CellTwoTab2 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        
        self.string =[[NSMutableAttributedString alloc]initWithString:model.title];
        
        
        
        [self.string addAttribute:NSForegroundColorAttributeName
                            value:[UIColor redColor]
                            range:[model.title rangeOfString:self.text.text]];
        
        cell.title.attributedText =self.string;
        cell.desc.text = model.desc;
        
        NSString *str = model.published;
        NSTimeInterval time=[str doubleValue];
        NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
        NSLog(@"date:%@",[detaildate description]);
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        
        NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
        
        NSLog(@"%@",currentDateStr);
        cell.time.text =  currentDateStr;
        
        [cell.image sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
        
        //            if ([model.weight_new isEqualToString:@"0"]) {
        //
        //                cell.image3.hidden = YES;
        //            }
        return cell;
    }
    
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSLog(@"%@",indexPath);
    NSLog(@"88888888888888");
    
     Model2 *model = [self.arr objectAtIndex:indexPath.row];
    
    if ([model.video_url isEqualToString:@""]) {
        NSLog(@"%@",model.idd);
        WebviewControl *web = [[WebviewControl alloc]init];
        
        NSString *str = [NSString stringWithFormat:@"http://lol.zhangyoubao.com/mobiles/item/%@?v_=400609&size=middle&t=1453899180",model.idd];
        web.urlstring = str;
        
        NSLog(@"%@",web.urlstring);
        [self.navigationController pushViewController:web animated:YES];
        
        [web release];
        
    }else{
        
        WebviewControl *web = [[WebviewControl alloc]init];
        
        NSString *str = model.video_url;
        web.urlstring = str;
        
        [self.navigationController pushViewController:web animated:YES];
        
        [web release];
        
    }
   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
